package com.lewyu.oasis.service.faced;


import com.alibaba.fastjson.JSONObject;
import com.github.benmanes.caffeine.cache.Cache;
import org.apache.commons.lang3.ThreadUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Component;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;

/**
 * @author lewyu
 * @date 2023年11月25日 21:57
 */
@RestController
public class MineFaced {

    @Autowired
    private Cache<Object, Object> caffeineCache;


    @PostMapping("/example")
    public ResponseEntity<JSONObject> example(@RequestBody JSONObject request) {
        request.get("hh");
        System.out.println("request: " + request);


        // Handle the request and return a response
        return ResponseEntity.ok(request);
    }

    @GetMapping("/hhhh")
    public ResponseEntity<JSONObject> hhhh() throws InterruptedException {
        System.out.println("hhhhhhhhhhhh");


        for (int i = 0; i < 10; i++) {
            caffeineCache.put(i, i);
        }

        for (int i = 0; i < 10; i++) {
            Thread.sleep(1000);
            System.out.println(caffeineCache.getIfPresent(i));
        }

        return ResponseEntity.ok(new JSONObject());
    }

    //url示例


//    public static void main(String[] args) {
//        StringBuffer sharedBuffer = new StringBuffer("Hello, world!");
//
//        // 创建第一个线程，替换字符串中的部分内容
//        Thread thread1 = new Thread(() -> {
//
//            synchronized (sharedBuffer) {
//                sharedBuffer.setLength(0);  // 清空原有内容
//                sharedBuffer.append("Hi, hhhhhhhhhhhhhhhhhhhhhhhh");
//            }
//        });
//
//        // 创建第二个线程，再次替换字符串中的部分内容
//        Thread thread2 = new Thread(() -> {
//        });
//
//        // 启动线程
//        thread1.start();
//        thread2.start();
//
//        try {
//            // 等待两个线程完成
//            thread1.join();
//            thread2.join();
//        } catch (InterruptedException e) {
//            e.printStackTrace();
//        }
//
//        // 打印最终结果
//        System.out.println("Final string: " + sharedBuffer);
//    }
}
